package com.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * SpringMVC配置类
 */
@Configuration//这是一个配置类
@EnableWebMvc//开启MVC默认配置，比如ViewResolver
@ComponentScan(basePackages = {"com.demo.controller", "com.demo.service"})
public class SpringMVCConfig implements WebMvcConfigurer {
    /**
     * 配置视图解析器
     */
    @Bean//在方法上 使用@Bean注解 则该方法的返回值会被添加到Spring容器中 以供依赖注入
    public InternalResourceViewResolver getViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    /**
     * 配置静态资源,不需要DispatcherServlet转发的请求
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
    }

}
